package com.example.musicplayer.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

public abstract class ServiceProxy {

    private IBinder mService;
    private ServiceConnection mServiceConnection = new ProxyConnection();

    public abstract void onConnected(ComponentName name, IBinder service);

    public abstract void onDisconnected(ComponentName name);

    public boolean isConnected() {
        if (mService != null) {
            return true;
        }

        return false;
    }

    protected void connect(Context context, Intent intent, int flags) {
        context.bindService(intent, mServiceConnection, flags);
    }

    private class ProxyConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = service;
            onConnected(name, service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            onDisconnected(name);
        }
    }
}
